package com.maomao.maomaochong2;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;

import java.util.ArrayList;

public class FourthActivity extends AppCompatActivity {
    public Context context;
    private MyTextView myTextView;
    private ListView listView;
    private MyListAdapter adapter;
    private FrameLayout fLayout;
    private GridFragment gFragment1,gFragment2,gFragment3,gFragment4,gFragment5,
            gFragment6,gFragment7,gFragment8,gFragment9,gFragment10,gFragment11,
            gFragment12,gFragment13;
    private FragmentManager fManager = null;
    private FragmentTransaction ft = null;
    private ArrayList<AllItem> gridData1, gridData2, gridData3, gridData4, gridData5,
                gridData6, gridData7, gridData8, gridData9, gridData10, gridData11,
                gridData12, gridData13;
    private long exitTime = 0;

    private String[] listName = {"全部商品", "趣品", "IPHONE7PLUS", "IPHONE7", "IPHONE6SPLUS",
            "IPHONE6S", "IPHONE6PLUS", "IPHONE6", "IPHONE5S", "IPHONE5C", "笔记本电脑", "平板电脑",
            "配件"};

    private int[] gridImage = {R.mipmap.bea4, R.mipmap.bea5, R.mipmap.bea2, R.mipmap.bea,
            R.mipmap.bea1, R.mipmap.bea7, R.mipmap.bea9, R.mipmap.img, R.mipmap.bea3,
            R.mipmap.bea6, R.mipmap.bea8, R.mipmap.bea10, R.mipmap.bea11};

    private String[] gridName = {"往事若能下酒，回忆便是一场宿醉", "做我女朋友吧，我的手指很长",
            "天青色等烟雨，而我在等你","\"用屁股想一下都知道了\"\n\"就喜欢你那会思考的小屁屁\"",
            "停车做爱枫林晚，隔江犹唱后庭花","落红不是无情物，化作春泥更护花",
            "竹杖芒鞋轻胜马，谁怕？一蓑烟雨任平生","如此良辰美景，兄台可要淫湿一手",
    "\"再追不到你，我就强暴你\"\n\"就算你得到我的肉体也得不到我的心\"\n\"得到肉体就够了,要心干什么\"\n\"得到我的心解锁更多姿势\"\n\"......\"","陌上花开蝴蝶飞，江山犹是昔人非",
    "问君能有几多愁，恰似一壶二锅头","曲径通幽处，禅房花木深",
    "你以三寸不烂之舌，吞我精兵两亿"};

    private String[] gridPrice = {"￥0","￥1", "￥2","￥3", "￥5","￥6", "￥8","￥12",
            "￥69","￥4","￥7","￥11","￥20"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fourth);
        //调用函数
        bindViews();

        //模拟一次点击，既进去后选择第一项
//        listView.performItemClick(listView.getChildAt(0), 0 ,
//                listView.getItemIdAtPosition(0));
        listView.performItemClick(listView.getAdapter().getView(0, null, null),
                0, listView.getItemIdAtPosition(0));
    }

    // UI组件初始化与事件绑定
    private void bindViews(){
        myTextView = (MyTextView) findViewById(R.id.myTextView);
        listView = (ListView) findViewById(R.id.listView);
        context = FourthActivity.this;
        fManager = getFragmentManager();
        fLayout = (FrameLayout) findViewById(R.id.frameView);

        //搜索框监听
        myTextView.setOnClickListener(listener);

        //ListView 监听
        adapter = new MyListAdapter(listName,context);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(itemListener);

    }

    //隐藏所有的Fragment
    private void hideAllFragment(FragmentTransaction ft){
        if (gFragment1 != null) ft.hide(gFragment1);
        if (gFragment2 != null) ft.hide(gFragment2);
        if (gFragment3 != null) ft.hide(gFragment3);
        if (gFragment4 != null) ft.hide(gFragment4);
        if (gFragment5 != null) ft.hide(gFragment5);
        if (gFragment6 != null) ft.hide(gFragment6);
        if (gFragment7 != null) ft.hide(gFragment7);
        if (gFragment8 != null) ft.hide(gFragment8);
        if (gFragment9 != null) ft.hide(gFragment9);
        if (gFragment10 != null) ft.hide(gFragment10);
        if (gFragment11 != null) ft.hide(gFragment11);
        if (gFragment12 != null) ft.hide(gFragment12);
        if (gFragment13 != null) ft.hide(gFragment13);
    }

    //点击搜索框
    public View.OnClickListener listener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(FourthActivity.this,MySearch.class);
                startActivity(intent);
        }
    };

    //点击侧边栏 ListView
    public AdapterView.OnItemClickListener itemListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            adapter.setSeclection(position);
            adapter.notifyDataSetChanged();
            //实现点击 listView显示相应的 fragment
            ft = fManager.beginTransaction();
            hideAllFragment(ft);
            switch (position){
                case 0:
                    if(gFragment1 == null){
                        //导入GridView中的数据
                        gridData1 = new ArrayList<AllItem>();
                        for (int i = 0; i < gridImage.length; i++) {
                            gridData1.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment1 = new GridFragment(fManager, gridData1, context);
                        ft.add(R.id.frameView, gFragment1);
                    } else {
                        ft.show(gFragment1);
                    }
                    break;

                case 1:
                    if(gFragment2 == null){
                        //导入GridView中的数据
                        gridData2 = new ArrayList<AllItem>();
                        for (int i = 0; i < position; i++) {
                            gridData2.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment2 = new GridFragment(fManager, gridData2, context);
                        ft.add(R.id.frameView, gFragment2);
                    } else {
                        ft.show(gFragment2);
                    }
                    break;

                case 2:
                    if(gFragment3 == null){
                        //导入GridView中的数据
                        gridData3 = new ArrayList<AllItem>();
                        for (int i = 0; i < position; i++) {
                            gridData3.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment3 = new GridFragment(fManager, gridData3, context);
                        ft.add(R.id.frameView, gFragment3);
                    } else {
                        ft.show(gFragment3);
                    }
                    break;

                case 3:
                    if(gFragment4 == null){
                        //导入GridView中的数据
                        gridData4 = new ArrayList<AllItem>();
                        for (int i = 0; i < position; i++) {
                            gridData4.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment4 = new GridFragment(fManager, gridData4, context);
                        ft.add(R.id.frameView, gFragment4);
                    } else {
                        ft.show(gFragment4);
                    }
                    break;

                case 4:
                    if(gFragment5 == null){
                        //导入GridView中的数据
                        gridData5 = new ArrayList<AllItem>();
                        for (int i = 0; i < position; i++) {
                            gridData5.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment5 = new GridFragment(fManager, gridData5, context);
                        ft.add(R.id.frameView, gFragment5);
                    } else {
                        ft.show(gFragment5);
                    }
                    break;

                case 5:
                    if(gFragment6 == null){
                        //导入GridView中的数据
                        gridData6 = new ArrayList<AllItem>();
                        for (int i = 0; i < position; i++) {
                            gridData6.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment6 = new GridFragment(fManager, gridData6, context);
                        ft.add(R.id.frameView, gFragment6);
                    } else {
                        ft.show(gFragment6);
                    }
                    break;

                case 6:
                    if(gFragment7 == null){
                        //导入GridView中的数据
                        gridData7 = new ArrayList<AllItem>();
                        for (int i = 0; i < position; i++) {
                            gridData7.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment7 = new GridFragment(fManager, gridData7, context);
                        ft.add(R.id.frameView, gFragment7);
                    } else {
                        ft.show(gFragment7);
                    }
                    break;

                case 7:
                    if(gFragment8 == null){
                        //导入GridView中的数据
                        gridData8 = new ArrayList<AllItem>();
                        for (int i = position; i > 6; i--) {
                            gridData8.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment8 = new GridFragment(fManager, gridData8, context);
                        ft.add(R.id.frameView, gFragment8);
                    } else {
                        ft.show(gFragment8);
                    }
                    break;

                case 8:
                    if(gFragment9 == null){
                        //导入GridView中的数据
                        gridData9 = new ArrayList<AllItem>();
                        for (int i = position; i > 5; i--) {
                            gridData9.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment9 = new GridFragment(fManager, gridData9, context);
                        ft.add(R.id.frameView, gFragment9);
                    } else {
                        ft.show(gFragment9);
                    }
                    break;

                case 9:
                    if(gFragment10 == null){
                        //导入GridView中的数据
                        gridData10 = new ArrayList<AllItem>();
                        for (int i = position; i > 5; i--) {
                            gridData10.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment10 = new GridFragment(fManager, gridData10, context);
                        ft.add(R.id.frameView, gFragment10);
                    } else {
                        ft.show(gFragment10);
                    }
                    break;

                case 10:
                    if(gFragment11 == null){
                        //导入GridView中的数据
                        gridData11 = new ArrayList<AllItem>();
                        for (int i = position; i > 2; i--) {
                            gridData11.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment11 = new GridFragment(fManager, gridData11, context);
                        ft.add(R.id.frameView, gFragment11);
                    } else {
                        ft.show(gFragment11);
                    }
                    break;

                case 11:
                    if(gFragment12 == null){
                        //导入GridView中的数据
                        gridData12 = new ArrayList<AllItem>();
                        for (int i = position; i > 3; i--) {
                            gridData12.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment12 = new GridFragment(fManager, gridData12, context);
                        ft.add(R.id.frameView, gFragment12);
                    } else {
                        ft.show(gFragment12);
                    }
                    break;

                case 12:
                    if(gFragment13 == null){
                        //导入GridView中的数据
                        gridData13 = new ArrayList<AllItem>();
                        for (int i = position; i > 10; i--) {
                            gridData13.add(new AllItem(gridImage[i], gridName[i], gridPrice[i]));
                        }
                        gFragment13 = new GridFragment(fManager, gridData13, context);
                        ft.add(R.id.frameView, gFragment13);
                    } else {
                        ft.show(gFragment13);
                    }
                    break;
            }
            ft.commit();
        }
    };
}
